import 'package:extended_image/extended_image.dart';
import 'package:flutter/material.dart';
import 'package:gd_assistant/globalConfig.dart';
import 'package:gd_assistant/model/userModel.dart';
import 'package:gd_assistant/page/me/changeMinePage.dart';
import 'package:gd_assistant/page/me/myCardPage.dart';
import 'package:gd_assistant/page/me/settingPage.dart';
import 'package:gd_assistant/page/public/colors.dart';
import 'package:gd_assistant/page/public/themeUtils.dart';
import 'package:gd_assistant/util/customRoute.dart';
import 'package:gd_assistant/api/apiHandler.dart';
import 'package:gd_assistant/util/dataUtil.dart';
import 'package:gd_assistant/util/eventBus.dart';

class minePage extends StatefulWidget {
  @override
  _minePageState createState() => _minePageState();
}

class _minePageState extends State<minePage> {
  bool isDark = false;
  Color bg_color;
  Color text_color;
  String avatar = "";
  String assistantName = "";
  String evaluationCount = "";
  String mark = "";
  @override
  void initState() {
    super.initState();
    loadData();
    EventBus().on("infoChange", (parmas){
      loadData();
    });
  }

  @override
  void dispose() {
    super.dispose();
    EventBus().off("infoChange");
  }

  loadData() async {
    avatar = dataUtil.getValue(dataUtil.assistantAvatarKey);
    assistantName = dataUtil.getValue(dataUtil.nickNameKey);
    evaluationCount = dataUtil.getValue(dataUtil.evaluationCountKey);
    mark = dataUtil.getValue(dataUtil.markKey);
    setState(() {});
  }

  Widget mycard() {
    return Padding(
      padding: EdgeInsets.fromLTRB(12, 0, 12, 0),
      child:  Container(
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.only(bottomLeft: Radius.circular(10), bottomRight: Radius.circular(10)),
                  color: bg_color
                ),
                height: 60,
                child: GestureDetector(
                  onTap: (){
                    Navigator.of(context).push(CustomRoute(myCardPage()));
                  },
                  child: Row(
                    children: <Widget>[
                      Padding(
                        padding: EdgeInsets.all(15),
                        child: Icon(Icons.business, color:Colors.blue),
                      ),
                      Expanded(
                        flex: 1,
                        child: Text("我的名片",style: TextStyle(fontSize: 17, color: text_color))
                      ),
                      Icon(Icons.arrow_forward_ios, size: 15, color: Colors.grey),
                      Padding(
                        padding: EdgeInsets.only(left: 12),
                      )
                    ],
                  ),
                )
              ),
    );
  }

  Widget setting() {
    return Padding(
      padding: EdgeInsets.fromLTRB(12, 50, 12, 0),
      child:  Container(
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.only(topLeft: Radius.circular(10), topRight: Radius.circular(10)),
                  color: bg_color
                ),
                height: 60,
                child: GestureDetector(
                  onTap: (){
                    Navigator.of(context).push(CustomRoute(settingPage()));
                  },
                  child: Row(
                    children: <Widget>[
                      Padding(
                        padding: EdgeInsets.all(15),
                        child: Icon(Icons.settings, color:Colors.blue),
                      ),
                      Expanded(
                        flex: 1,
                        child: Text("设置",style: TextStyle(fontSize: 17, color: text_color))
                      ),
                      Icon(Icons.arrow_forward_ios, size: 15, color: Colors.grey),
                      Padding(
                        padding: EdgeInsets.only(left: 12),
                      )
                    ],
                  ),
                )
              ),
    );
   
  }

  Widget header() {
    return Container(
      height: 280,
      padding: EdgeInsets.fromLTRB(12, 0, 12, 0),
      decoration: BoxDecoration(
        image: DecorationImage(
          image: ExactAssetImage(isDark ? "images/dark_bg.jpg" : "images/mybg.png"),
          fit: BoxFit.cover
        )
      ),
      child: OverflowBox(
        alignment: Alignment.topCenter,
        maxHeight: 310,
        child: Container(
          height: 310,
          child: Column(
            children: <Widget>[
              Expanded(
                flex: 1,
                child: Container(
                  child: Row(
                    children: <Widget>[
                      Container(
                        width: 150,
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: <Widget>[
                            Container(
                              padding: EdgeInsets.fromLTRB(0, 110, 0, 0),
                              child: Container(
                                width: 72,
                                height: 72,
                                decoration: BoxDecoration(
                                  border: Border.all(color: Colors.white, width: 2),
                                  borderRadius: BorderRadius.circular(36)
                                ),
                                child: ClipOval(
                                  child: ExtendedImage.network(
                                    GlobalConfig.qiNiuUri + (avatar != null ? avatar : ""),
                                    width: 70,
                                    height: 70,
                                    fit: BoxFit.fill,
                                  ),
                                ),
                              )
                            ),
                            Padding(
                              padding: EdgeInsets.only(top: 10),
                              child: Text(assistantName != null ? assistantName : "", style: TextStyle(fontSize: 18,color: Colors.white)),
                            )
                          ],
                        ),
                      ),
                      Expanded(
                        flex: 1,
                        child: GestureDetector(
                          onTap: (){
                            Navigator.of(context).push(CustomRoute(changeMinePage()));
                          },
                          child: Container(
                            alignment: Alignment.bottomRight,
                            padding: EdgeInsets.fromLTRB(0, 0, 20, 30),
                            child: Container(
                              width: 30,
                              height: 30,
                              child: Image.asset("images/write.png"),
                            ),
                          ),
                        )
                      )
                    ],
                  ),
                )
              ),
              Container(
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.all(Radius.circular(10)),
                  color: bg_color
                ),
                height: 60,
                child: Row(
                  children: <Widget>[
                    Padding(
                      padding: EdgeInsets.all(15),
                      child: Icon(Icons.event_available, color: Colors.yellow[700]),
                    ),
                    Expanded(
                      flex: 1,
                      child: RichText(
                        text: TextSpan(
                          text: "评价次数: ",
                          style: TextStyle(fontSize: 17, color: text_color),
                          children: <TextSpan>[
                            TextSpan(
                              text: evaluationCount.toString(),
                              style: TextStyle(color: Colors.orange)
                            )
                          ]
                        ),
                      ),
                    ),
                    Expanded(
                      flex: 1,
                      child: Padding(
                        padding: EdgeInsets.only(right: 50),
                        child: RichText(
                          textAlign: TextAlign.right,
                          text: TextSpan(
                            text: "平均得分: ",
                            style: TextStyle(fontSize: 17, color: text_color),
                            children: <TextSpan>[
                              TextSpan(
                                text: mark.toString(),
                                style: TextStyle(color: Colors.orange)
                              )
                            ]
                          ),
                        ),
                      )
                    )
                  ],
                ),
              )
            ],
          ),
        ),
      )
    );
  }
  Widget build(BuildContext context) {
    isDark = ThemeUtils.isDark(context);
    bg_color = isDark ? Colours.dark_bg_gray : Colors.white;
    text_color = isDark ? Colors.white : Colors.black;
    return new Scaffold(
      backgroundColor: isDark ? Colours.dark_bg_color : Colours.bg_color,
      body: MediaQuery.removePadding(
        context: context,
        removeTop: true,
        child:  ListView(
          children: <Widget>[
            header(),
            setting(),
            mycard()
          ],
        ),
      )
    );
  }

}